package org.veridiandynamics.server.dao;
import org.veridiandynamics.server.domain.*;

import java.util.*;
/**
 * This tests the database.
 *
 */
public class TestMain {
	/**
	 * Main method to drive tests.
	 */
	public static void main(String args[]){
		
		String query1="CREATE DATABASE TEST2";
		String query2="CREATE TABLE example7(id INT AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT)";
		String query3="CREATE TABLE Users(userid INT AUTO_INCREMENT, PRIMARY KEY(userid), user_name VARCHAR(30), password VARCHAR(32), first_name VARCHAR(30), last_name VARCHAR(30), age INT, address VARCHAR(50), city VARCHAR(30), state VARCHAR(30), zipcode VARCHAR(30), email VARCHAR(30), phone_num VARCHAR(30))";
		String query4="CREATE TABLE Roles(roleid INT AUTO_INCREMENT, PRIMARY KEY(roleid), userid INT NOT NULL, role VARCHAR(15) NOT NULL)";
		String query5="CREATE TABLE Patients(patientid INT AUTO_INCREMENT, PRIMARY KEY(patientid), userid INT NOT NULL, doctorid INT, pharmacy VARCHAR(40), insurance VARCHAR(40), allergies VARCHAR(100))";
//		String query3="CREATE TABLE Roles7(PRIMARY KEY(userid,role), userid INT NOT NULL, role VARCHAR(15) NOT NULL)";
		Queryprocessor.RunUpdate(query3);
		
		/*
		ArrayList mykeys;
		mykeys=Queryprocessor.IdQuery("SELECT uid FROM USER");
		for (int i=0; i<mykeys.size(); i++)
		{
			//System.out.println(mykeys.get(i));
		} */
		HibernateDriver myhdriver = new HibernateDriver();
		HibernateDriver my2ndhdriver = new HibernateDriver();
		
		Event myevent = new Event();
		
		myhdriver.SaveObject(myevent);
		/*myevent=(Event)myhdriver.RetrieveObject(24, myevent);
		myevent.setTitle("test1");
		myhdriver.UpdateObject(myevent);
		myhdriver.DeleteObject(myevent);
		System.out.println(myevent.getTitle()); */
		Nurse mynurse = new Nurse();
		Admin myadmin= new Admin();
//		myadmin.setPassword("password1");
//		myadmin.setUsername("testAdmin");
		Doctor mydoctor=new Doctor();
//		mydoctor.setPassword("password1");
//		myadmin.setUsername("testDoctor");
		Patient mypatient=new Patient();
		Invoice myinvoice=new Invoice();
		Invoice myinvoice2=new Invoice();
		mynurse.setMyRoles(new HashSet<Roles>());
		//mynurse.setUsername("test1");
		//mynurse.setPassword("password1");
//		mypatient.setUsername("testPatient");
//		mypatient.setPassword("password1");
//		Doctor doctorNew = new Doctor();
//		doctorNew.setUsername("testDoctor4");
//		doctorNew.setPassword("password1");
//		doctorNew.setMyRoles(new HashSet<Roles>());
//		doctorNew.getMyRoles().add(new Roles(RoleLookup.Role.DOCTOR));
//		myhdriver.SaveObject(doctorNew);
		
		Admin admin = new Admin();
		//admin.setUsername("testAdmin");
		admin.setPassword("password1");
		admin.setMyRoles(new HashSet<Roles>());
		admin.getMyRoles().add(new Roles(RoleLookup.Role.ADMIN));
		myhdriver.SaveObject(admin);
		
		mynurse.getMyRoles().add(new Roles(RoleLookup.Role.NURSE));
		mynurse.getMyRoles().add(new Roles(RoleLookup.Role.ADMIN));
		mypatient.setMyRoles(new HashSet<Roles>());
		mypatient.getMyRoles().add(new Roles(RoleLookup.Role.PATIENT));
		mypatient.setDoctor(mydoctor);
		mypatient.setMyAppointments(new HashSet<Appointment>());
		mypatient.getMyAppointments().add(new Appointment());
		mypatient.setMyTreatmentRecordList(new HashSet<TreatmentRecord>());
		mypatient.getMyTreatmentRecordList().add(new TreatmentRecord());
		mypatient.getMyTreatmentRecordList().add(new TreatmentRecord());

		myinvoice.setServicesList(new HashSet<ServicesBillable>());
		myinvoice.getServicesList().add(new ServicesBillable());
		Patient mypatient2= new Patient();
		mypatient2.setDoctor(mydoctor);
		myinvoice.setMyDoctor(mydoctor);
		myinvoice2.setMyDoctor(mydoctor);
		
		myhdriver.SaveObject(mynurse);
		myhdriver.SaveObject(myadmin);
		myhdriver.SaveObject(mydoctor);
		myhdriver.SaveObject(mypatient);

		myhdriver.SaveObject(myinvoice);
		my2ndhdriver.SaveObject(myinvoice2);
		myhdriver.SaveObject(mypatient2);
		
		mydoctor=(Doctor)myhdriver.RetrieveObject(3, mydoctor);
		mydoctor.getMySchedule().addAppointment(new Appointment());
		Set<Appointment> myappset=new HashSet<Appointment>(0);
		myappset=mydoctor.getMySchedule().getMySchedule();
		Appointment[] myapp=myappset.toArray(new Appointment[0]);
		myapp[0].setReason("test");
		myapp[0].setStartDate(new Date());
		//myapp[1].setReason("hi");
//		mypatient=(Patient)myhdriver.RetrieveObject(16, mypatient);
		mynurse.setFirstName("Talia");
		mynurse.setMiddleName("queen");
		mynurse.setLastName("bee");
		myhdriver.UpdateObject(mynurse);
		mypatient.setFirstName("henry");
		mypatient.setMiddleName("Jones");
		mypatient.setLastName("smith");
		mypatient.setMyTreatmentRecordList(new HashSet<TreatmentRecord>());
		TreatmentRecord[] mytreats=mypatient.getMyTreatmentRecordList().toArray(new TreatmentRecord[0]);
		DoctorsOrders orders = new DoctorsOrders("drink fluids", "xanax", "none", "6 months");
		Map<String, String> vitalSigns = new HashMap<String, String>();
		vitalSigns.put("blood pressure", "adsfa");
		vitalSigns.put("pulse", "2323");
		vitalSigns.put("weight", "232");
		vitalSigns.put("height", "asdfa");
		vitalSigns.put("temperature", "3455");
		TreatmentRecord tRecord1 = new TreatmentRecord(mynurse, orders, "sick", vitalSigns, "dead", mypatient, new Date());
		mypatient.getMyTreatmentRecordList().add(tRecord1);
		myhdriver.UpdateObject(mypatient);
		//mytreats[1].setDoctorsOrders(new DoctorsOrders());
		myhdriver.UpdateObject(mydoctor);
		myhdriver.UpdateObject(mypatient);
		System.out.println(mydoctor.getMySchedule().getId());
		System.out.println(Queryprocessor.UsernametoID("test"));
		
		Nurse lognurse=new Nurse();
		//failure test
		lognurse=(Nurse)myhdriver.LoginRetrieve("test1","password2",new Roles(RoleLookup.Role.NURSE));
		if (lognurse==null){System.out.println("null object");}
		
		//success test
		lognurse=(Nurse)myhdriver.LoginRetrieve("test1","password1",new Roles(RoleLookup.Role.NURSE));
		System.out.println(lognurse.getUsername());
		ExceptionLogger.Append("test exception");

		/*
		List<Patient> mylist;
		mylist=myhdriver.GetAll(mypatient);
		for (int i=0; i<mylist.size();i++)
		{
					System.out.println("pid"+mylist.get(i).getId());
		}
		List<Doctor> mylist2;
		mylist2=myhdriver.GetAll(mydoctor);
		for (int i=0; i<mylist2.size();i++)
		{
					System.out.println("doc id"+mylist2.get(i).getId());
		}
		List<Invoice> mylist3;
		mylist3=myhdriver.GetAll(myinvoice);
		for (int i=0; i<mylist3.size();i++)
		{
					System.out.println("invoice id"+mylist3.get(i).getId());
		}*/
		
		List<MyException> mylist4;
		mylist4=myhdriver.GetAll(new MyException());
		for (int i=0; i<mylist4.size();i++)
		{
					System.out.println("Exception id"+mylist4.get(i).getId());
		}
		myhdriver.CloseConnection();
		my2ndhdriver.CloseConnection();
	}
}
